home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / UnicastRemoteObject.java < prev    next >
Text File  |  1998-09-22  |  3KB  |  100 lines

  1. /*
  2.  * @(#)UnicastRemoteObject.java    1.11 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.rmi.server;
  15.  
  16. import java.rmi.*;
  17.  
  18. /**
  19.  * The UnicastRemoteObject class defines a non-replicated remote
  20.  * object whose references are valid only while the server process is
  21.  * alive.  The UnicastRemoteObject class provides support for
  22.  * point-to-point active object references (invocations, parameters,
  23.  * and results) using TCP streams.  <p>
  24.  *
  25.  * Objects that require remote behavior should extend RemoteObject,
  26.  * typically via UnicastRemoteObject. If UnicastRemoteObject is not
  27.  * extended, the implementation class must then assume the
  28.  * responsibility for the correct semantics of the hashCode, equals,
  29.  * and toString methods inherited from the Object class, so that they
  30.  * behave appropriately for remote objects.
  31.  */
  32. public class UnicastRemoteObject extends RemoteServer {
  33.  
  34.  
  35.     /**
  36.      * Create and export a new UnicastRemoteObject object using an
  37.      * anonymous port.
  38.      */
  39.     protected UnicastRemoteObject() throws RemoteException
  40.     {
  41.     exportObject((Remote)this);
  42.     }
  43.  
  44.     /**
  45.      * Re-export the remote object when it is deserialized.
  46.      */
  47.     private void readObject(java.io.ObjectInputStream in) 
  48.     throws java.io.IOException, java.lang.ClassNotFoundException
  49.     {
  50.     exportObject((Remote)this);
  51.     }
  52.     
  53.     /**
  54.      * Returns a clone of the remote object that is distinct from
  55.      * the original.
  56.      *
  57.      * @exception CloneNotSupportedException if clone failed due to
  58.      * a RemoteException.
  59.      * @return the new remote object
  60.      */
  61.     public Object clone() throws CloneNotSupportedException
  62.     {
  63.     try {
  64.         UnicastRemoteObject remote = (UnicastRemoteObject)super.clone();
  65.         exportObject(remote);
  66.         return remote;
  67.     } catch (RemoteException e) {
  68.         throw new ServerCloneException("Clone failed", e);
  69.     }
  70.     }
  71.  
  72.     /** 
  73.      * Export the remote object to make it available to receive incoming calls.
  74.      * @param obj the remote object to be exported
  75.      * @exception RemoteException if export fails
  76.      */
  77.     public static RemoteStub exportObject(Remote obj)
  78.     throws RemoteException
  79.     {
  80.     /* Server ref must be created and assigned before remote object 
  81.      * can be exported.
  82.      */
  83.     try {
  84.         Class refClass = Class.forName(RemoteRef.packagePrefix +
  85.                        ".UnicastServerRef");
  86.         Object refObj = refClass.newInstance();
  87.         if (refObj instanceof ServerRef) {
  88.         ServerRef serverRef = (ServerRef)refObj;
  89.         if (obj instanceof UnicastRemoteObject)
  90.             ((UnicastRemoteObject)obj).ref = serverRef;
  91.         return serverRef.exportObject(obj, null);
  92.         } else {
  93.         throw new ExportException("Reference is not a java.rmi.server.ServerRef");
  94.         }
  95.     } catch (Exception e) {
  96.         throw new ExportException("Unable to create remote reference", e);
  97.     }
  98.     }
  99. }
  100.